<template>
    <div>
        <div v-show="visiable">
            <p>绑定用户名：<input type="text" v-model="username" @blur="username_id"></p>
            <p v-show='username_j' style="color:green">{{ username_message }}</p>
            <p>密  码：<input type="password" v-model="password"></p>
            <button  @click="bindUser">绑 定</button>
        </div>
    </div>
</template>

<script>
import {oauth_callback_post,oauth_bind_post,unique_post} from '../axios_api/api'
export default {
data() {
    return {
       visiable:false,
       username:"",
       password:"",
       uid:'', 
       username_message:"",
       username_j:false,

    }
},
methods: {
 getCode(){
     //获取url中的code中信息
     //http://127.0.0.1:8080/oauth/?code=c07d047273fa18e5a5419436a9671d32
    let code = this.$route.query.code
    console.log(this.$route.query)
    //给后端发送code获取acc_token
    oauth_callback_post({code:code}).then(res=>{
        console.log(res)
        if(res.data.type=="0"){
        //authenticated: "true"
        //email: ""
        //id: 1
        //name: "admin"
        //role: null
        //token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTk3OTIxMjUyLCJlbWFpbCI6IiIsIm9yaWdfaWF0IjoxNTk3ODM0ODUyfQ.aWQ9LTu9Ae_nmGcZswNp1k5M-Vy3bk_Ee55Z_O0NUHs"
        //type: "0"
        //username: "admin"
        localStorage.setItem('username', res.data.username)
        // localStorage.setItem('img', res.data.img)
        localStorage.setItem('token', res.data.token)
        localStorage.setItem('uid', res.data.id)
        this.login_username = res.data.username
        this.opened = false
        this.$router.push('/')
        }
        if(res.data.type=="1"){
            this.visiable = true
            this.uid = res.data.uid
        }
    })

 },
 bindUser(){
     oauth_bind_post({"username":this.username,"password":this.password,"weibo_uid":this.uid}).then(res=>{
         console.log(res)
        localStorage.setItem('username', res.data.username)
        // localStorage.setItem('img', res.img)
        localStorage.setItem('token', res.data.token)
        localStorage.setItem('uid', res.data.id)
        this.login_username = res.data.username
        this.opened = false
        // alert(res.message)
        this.$router.push('/')

     })

 },
 username_id() {
      console.log('判断用户名')
      console.log(this.username == '')
      var reg = new RegExp(/^[a-zA-Z0-9_-]{4,16}$/);
      if (this.username == '') {
        this.username_message = '用户名不能为空'
        this.username_j = true
      } else if (!reg.test(this.username)) //检测字符串是否符合正则表达式 4到14位（字母，数字，下划线，减号）
      {
        this.username_message = '用户名格式不正确'
        this.username_j = true
        return false
      };
      unique_post({"type":"username","data":this.username}).then(res=>{
        console.log(res)
        if(res.count>0){
          
          this.username_message="用户名已有，请输入密码进行绑定"
          this.username_j = true
        }else{
        this.username_message = '用户名可用, 将创建新用户,请输入密码'
        this.username_j = true
         
        }
      })
      
    },   

},
created() {
   this.getCode() 
},
}
</script>

<style>

</style>
